package com.atguigu.gmallre.mock.service.impl;


import com.atguigu.gmallre.mock.bean.*;
import com.atguigu.gmallre.mock.config.AppConfig;
import com.atguigu.gmallre.mock.factory.UserSession;
import com.atguigu.gmallre.mock.mapper.*;
import com.atguigu.gmallre.mock.service.*;
import com.atguigu.gmallre.mock.service.adv.impl.AdvServiceImpl;
import com.atguigu.mock.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zc
 * @since 2020-02-23
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends AdvServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    UserInfoMapper userInfoMapper;






    @Value("${mock.user.update-rate:20}")
    String updateRate;

    public List<UserInfo>  genUserInfos( Integer count, Boolean ifClear){


        Date date = AppConfig.mock_date;

        List<UserInfo>  userInfoList=new ArrayList<>();
        if(ifClear){
            userInfoMapper.truncateUserInfo();
        }else{
            updateUsers(  date);
        }

        for (int i = 0; i < count; i++) {
            userInfoList.add( initUserInfo(date)) ;
        }
        if(userInfoList.size()>0){
            saveBatch(userInfoList,1000);
            fixUserInfo(  userInfoList);
            saveOrUpdateBatch(userInfoList);
        }
        log.warn("共生成{}名用户",userInfoList.size());

        loadCache();
        return userInfoList;

    }

    public   UserInfo   genUserInfo(){
        UserInfo userInfo = initUserInfo(UserSession.getCurDateTime());
        save(userInfo);
        cache.put(userInfo.getId(),userInfo);
        log.warn("注册1个用户" );
        return userInfo;
    }

    public UserInfo initUserInfo(Date date){


        UserInfo userInfo = new UserInfo();
        String email = RandomEmail.getEmail(6, 12);
        String loginName = email.split("@")[0];
        userInfo.setLoginName(loginName);
        userInfo.setEmail(email);
        userInfo.setBirthday(DateUtils.addMonths(date, -1*RandomNum.getRandInt(15*12,55*12)));

        userInfo.setCreateTime(date);
        userInfo.setUserLevel(new RandomBox(new RanOpt("1",7),new RanOpt("2",2),new RanOpt("3",1)).getRandStringValue());
        userInfo.setPhoneNum("13"+RandomNumString.getRandNumString(1,9,9,""));
        return userInfo;
    }

    public void fixUserInfo(List<UserInfo> userInfoList){
        for (UserInfo userInfo : userInfoList) {
            String gender="";
            if(userInfo.getId()%2==0){
                gender="F";
            }else{
                gender="M";
            }
            if(userInfo.getId()%3!=0){
                userInfo.setGender(gender);
            }
            String lastName = RandomName.insideLastName(gender);
            userInfo.setName(RandomName.getChineseFamilyName()+lastName);
            userInfo.setNickName(RandomName.getNickName(gender,lastName));
        }

    }

    public void  updateUsers(Date date){


        Integer updateRateWeight = ParamUtil.checkRatioNum(this.updateRate);
        if (updateRateWeight==0){
            return ;
        }

        int count = count(new QueryWrapper<UserInfo>());

        String userIds = RandomNumString.getRandNumString(1, count, count *  updateRateWeight/ 100, ",",false);

        List<UserInfo> userInfoList = list(new QueryWrapper<UserInfo>().inSql("id", userIds));
        for (UserInfo userInfo : userInfoList) {
            int randInt = RandomNum.getRandInt(2, 7);
            String gender="";
            if(userInfo.getId()%2==0){
                gender="F";
            }else{
                gender="M";
            }
            if (randInt%2==0){
                String lastName = RandomName.insideLastName(gender);
                userInfo.setNickName(RandomName.getNickName(gender,lastName));
            }
            if(randInt%3==0){
                userInfo.setUserLevel(new RandomBox(new RanOpt("1",7),new RanOpt("2",2),new RanOpt("3",1)).getRandStringValue());
            }
            if(randInt%5==0){
                String email = RandomEmail.getEmail(6, 12);
                userInfo.setEmail(email);
            }
            if(randInt%7==0){
                userInfo.setPhoneNum("13"+RandomNumString.getRandNumString(1,9,9,""));
            }
            userInfo.setOperateTime(date);
        }
        log.warn("共有{}名用户发生变更",userInfoList.size());
        saveOrUpdateBatch(userInfoList);
        loadCache();



    }

}
